home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / emacs / 19.22 / lisp / subr.el < prev    next >
Lisp/Scheme  |  1993-11-23  |  25KB  |  671 lines

  1. ;;; subr.el --- basic lisp subroutines for Emacs
  2.  
  3. ;;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  19. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. ;;; Code:
  22.  
  23.  
  24. ;;;; Lisp language features.
  25.  
  26. (defmacro lambda (&rest cdr)
  27.   "Return a lambda expression.
  28. A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
  29. self-quoting; the result of evaluating the lambda expression is the
  30. expression itself.  The lambda expression may then be treated as a
  31. function, i. e. stored as the function value of a symbol, passed to
  32. funcall or mapcar, etcetera.
  33. ARGS should take the same form as an argument list for a `defun'.
  34. DOCSTRING should be a string, as described for `defun'.  It may be omitted.
  35. INTERACTIVE should be a call to the function `interactive', which see.
  36. It may also be omitted.
  37. BODY should be a list of lisp expressions."
  38.   ;; Note that this definition should not use backquotes; subr.el should not
  39.   ;; depend on backquote.el.
  40.   (list 'function (cons 'lambda cdr)))
  41.  
  42. ;;(defmacro defun-inline (name args &rest body)
  43. ;;  "Create an \"inline defun\" (actually a macro).
  44. ;;Use just like `defun'."
  45. ;;  (nconc (list 'defmacro name '(&rest args))
  46. ;;     (if (stringp (car body))
  47. ;;         (prog1 (list (car body))
  48. ;;           (setq body (or (cdr body) body))))
  49. ;;     (list (list 'cons (list 'quote
  50. ;;                 (cons 'lambda (cons args body)))
  51. ;;             'args))))
  52.  
  53.  
  54. ;;;; Window tree functions.
  55.  
  56. (defun one-window-p (&optional nomini)
  57.   "Returns non-nil if there is only one window.
  58. Optional arg NOMINI non-nil means don't count the minibuffer
  59. even if it is active."
  60.   (let ((base-window (selected-window)))
  61.     (if (and nomini (eq base-window (minibuffer-window)))
  62.     (setq base-window (next-window base-window)))
  63.     (eq base-window
  64.     (next-window base-window (if nomini 'arg)))))
  65.  
  66. (defun walk-windows (proc &optional minibuf all-frames)
  67.   "Cycle through all visible windows, calling PROC for each one.
  68. PROC is called with a window as argument.
  69. Optional second arg MINIBUF t means count the minibuffer window
  70. even if not active.  If MINIBUF is neither t nor nil it means
  71. not to count the minibuffer even if it is active.
  72.  
  73. Optional third arg ALL-FRAMES, if t, means include all frames.
  74. ALL-FRAMES nil or omitted means cycle within the selected frame,
  75. but include the minibuffer window (if MINIBUF says so) that that
  76. frame uses, even if it is on another frame.
  77. If ALL-FRAMES is neither nil nor t, stick strictly to the selected frame."
  78.   ;; If we start from the minibuffer window, don't fail to come back to it.
  79.   (if (window-minibuffer-p (selected-window))
  80.       (setq minibuf t))
  81.   (let* ((walk-windows-start (selected-window))
  82.      (walk-windows-current walk-windows-start))
  83.     (while (progn
  84.          (setq walk-windows-current
  85.            (next-window walk-windows-current minibuf all-frames))
  86.          (funcall proc walk-windows-current)
  87.          (not (eq walk-windows-current walk-windows-start))))))
  88.  
  89. (defun minibuffer-window-active-p (window)
  90.   "Return t if WINDOW (a minibuffer window) is now active."
  91.   ;; nil nil means include WINDOW's frame
  92.   ;; and other frames using WINDOW as minibuffer,
  93.   ;; and include minibuffer if active.
  94.   (let ((prev (previous-window window nil nil)))
  95.     ;; If PREV equals WINDOW, WINDOW must be on a minibuffer-only frame
  96.     ;; and it's not currently being used.  So return nil.
  97.     (and (not (eq window prev))
  98.      (let ((should-be-same (next-window prev nil nil)))
  99.        ;; If next-window doesn't reverse previous-window,
  100.        ;; WINDOW must be outside the cycle specified by nil nil.
  101.        (eq should-be-same window)))))
  102.  
  103. ;;;; Keymap support.
  104.  
  105. (defun undefined ()
  106.   (interactive)
  107.   (ding))
  108.  
  109. ;Prevent the \{...} documentation construct
  110. ;from mentioning keys that run this command.
  111. (put 'undefined 'suppress-keymap t)
  112.  
  113. (defun suppress-keymap (map &optional nodigits)
  114.   "Make MAP override all normally self-inserting keys to be undefined.
  115. Normally, as an exception, digits and minus-sign are set to make prefix args,
  116. but optional second arg NODIGITS non-nil treats them like other chars."
  117.   (substitute-key-definition 'self-insert-command 'undefined map global-map)
  118.   (or nodigits
  119.       (let (loop)
  120.     (define-key map "-" 'negative-argument)
  121.     ;; Make plain numbers do numeric args.
  122.     (setq loop ?0)
  123.     (while (<= loop ?9)
  124.       (define-key map (char-to-string loop) 'digit-argument)
  125.       (setq loop (1+ loop))))))
  126.  
  127. ;Moved to keymap.c
  128. ;(defun copy-keymap (keymap)
  129. ;  "Return a copy of KEYMAP"  
  130. ;  (while (not (keymapp keymap))
  131. ;    (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
  132. ;  (if (vectorp keymap)
  133. ;      (copy-sequence keymap)
  134. ;      (copy-alist keymap)))
  135.  
  136. (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
  137.   "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
  138. In other words, OLDDEF is replaced with NEWDEF where ever it appears.
  139. If optional fourth argument OLDMAP is specified, we redefine
  140. in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
  141.   (or prefix (setq prefix ""))
  142.   (let* ((scan (or oldmap keymap))
  143.      (vec1 (vector nil))
  144.      (prefix1 (vconcat prefix vec1)))
  145.     ;; Scan OLDMAP, finding each char or event-symbol that
  146.     ;; has any definition, and act on it with hack-key.
  147.     (while (consp scan)
  148.       (if (consp (car scan))
  149.       (let ((char (car (car scan)))
  150.         (defn (cdr (car scan))))
  151.         ;; The inside of this let duplicates exactly
  152.         ;; the inside of the following let that handles array elements.
  153.         (aset vec1 0 char)
  154.         (aset prefix1 (length prefix) char)
  155.         (let (inner-def)
  156.           ;; Skip past menu-prompt.
  157.           (while (stringp (car-safe defn))
  158.         (setq defn (cdr defn)))
  159.           (setq inner-def defn)
  160.           (while (and (symbolp inner-def)
  161.               (fboundp inner-def))
  162.         (setq inner-def (symbol-function inner-def)))
  163.           (if (eq defn olddef)
  164.           (define-key keymap prefix1 newdef)
  165.         (if (keymapp defn)
  166.             (substitute-key-definition olddef newdef keymap
  167.                            inner-def
  168.                            prefix1)))))
  169.     (if (arrayp (car scan))
  170.         (let* ((array (car scan))
  171.            (len (length array))
  172.            (i 0))
  173.           (while (< i len)
  174.         (let ((char i) (defn (aref array i)))
  175.           ;; The inside of this let duplicates exactly
  176.           ;; the inside of the previous let.
  177.           (aset vec1 0 char)
  178.           (aset prefix1 (length prefix) char)
  179.           (let (inner-def)
  180.             ;; Skip past menu-prompt.
  181.             (while (stringp (car-safe defn))
  182.               (setq defn (cdr defn)))
  183.             (setq inner-def defn)
  184.             (while (and (symbolp inner-def)
  185.                 (fboundp inner-def))
  186.               (setq inner-def (symbol-function inner-def)))
  187.             (if (eq defn olddef)
  188.             (define-key keymap prefix1 newdef)
  189.               (if (keymapp defn)
  190.               (substitute-key-definition olddef newdef keymap
  191.                              inner-def
  192.                              prefix1)))))
  193.         (setq i (1+ i))))))
  194.       (setq scan (cdr scan)))))
  195.  
  196. (defun define-key-after (keymap key definition after)
  197.   "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
  198. This is like `define-key' except that the binding for KEY is placed
  199. just after the binding for the event AFTER, instead of at the beginning
  200. of the map.
  201. The order matters when the keymap is used as a menu.
  202. KEY must contain just one event type--it must be a string or vector
  203. of length 1."
  204.   (or (keymapp keymap)
  205.       (signal 'wrong-type-argument (list 'keymapp keymap)))
  206.   (if (> (length key) 1)
  207.       (error "multi-event key specified in `define-key-after'"))
  208.   (let ((tail keymap) done inserted
  209.     (first (aref key 0)))
  210.     (while (and (not done) tail)
  211.       ;; Delete any earlier bindings for the same key.
  212.       (if (eq (car-safe (car (cdr tail))) first)
  213.       (setcdr tail (cdr (cdr tail))))
  214.       ;; When we reach AFTER's binding, insert the new binding after.
  215.       ;; If we reach an inherited keymap, insert just before that.
  216.       ;; If we reach the end of this keymap, insert at the end.
  217.       (if (or (eq (car-safe (car tail)) after)
  218.           (eq (car (cdr tail)) 'keymap)
  219.           (null (cdr tail)))
  220.       (progn
  221.         ;; Stop the scan only if we find a parent keymap.
  222.         ;; Keep going past the inserted element
  223.         ;; so we can delete any duplications that come later.
  224.         (if (eq (car (cdr tail)) 'keymap)
  225.         (setq done t))
  226.         ;; Don't insert more than once.
  227.         (or inserted
  228.         (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
  229.         (setq inserted t)))
  230.       (setq tail (cdr tail)))))
  231.  
  232. (defun keyboard-translate (from to)
  233.   "Translate character FROM to TO at a low level.
  234. This function creates a `keyboard-translate-table' if necessary
  235. and then modifies one entry in it."
  236.   (or (arrayp keyboard-translate-table)
  237.       (setq keyboard-translate-table ""))
  238.   (if (or (> from (length keyboard-translate-table))
  239.       (> to   (length keyboard-translate-table)))
  240.       (progn
  241.     (let* ((i (length keyboard-translate-table))
  242.            (table (concat keyboard-translate-table
  243.                   (make-string (- 256 i) 0))))
  244.       (while (< i 256)
  245.         (aset table i i)
  246.         (setq i (1+ i)))
  247.       (setq keyboard-translate-table table))))
  248.   (aset keyboard-translate-table from to))
  249.  
  250.  
  251. ;;;; The global keymap tree.  
  252.  
  253. ;;; global-map, esc-map, and ctl-x-map have their values set up in
  254. ;;; keymap.c; we just give them docstrings here.
  255.  
  256. (defvar global-map nil
  257.   "Default global keymap mapping Emacs keyboard input into commands.
  258. The value is a keymap which is usually (but not necessarily) Emacs's
  259. global map.")
  260.  
  261. (defvar esc-map nil
  262.   "Default keymap for ESC (meta) commands.
  263. The normal global definition of the character ESC indirects to this keymap.")
  264.  
  265. (defvar ctl-x-map nil
  266.   "Default keymap for C-x commands.
  267. The normal global definition of the character C-x indirects to this keymap.")
  268.  
  269. (defvar ctl-x-4-map (make-sparse-keymap)
  270.   "Keymap for subcommands of C-x 4")
  271. (defalias 'ctl-x-4-prefix ctl-x-4-map)
  272. (define-key ctl-x-map "4" 'ctl-x-4-prefix)
  273.  
  274. (defvar ctl-x-5-map (make-sparse-keymap)
  275.   "Keymap for frame commands.")
  276. (defalias 'ctl-x-5-prefix ctl-x-5-map)
  277. (define-key ctl-x-map "5" 'ctl-x-5-prefix)
  278.  
  279.  
  280. ;;;; Event manipulation functions.
  281.  
  282. ;; This code exists specifically to make sure that the
  283. ;; resulting number does not appear in the .elc file.
  284. ;; The number is negative on most machines, but not on all!
  285. (defconst listify-key-sequence-1
  286.    (lsh 1 7))
  287. (setq listify-key-sequence-1 (logior (lsh 1 23) listify-key-sequence-1))
  288.  
  289. (defun listify-key-sequence (key)
  290.   "Convert a key sequence to a list of events."
  291.   (if (vectorp key)
  292.       (append key nil)
  293.     (mapcar (function (lambda (c)
  294.             (if (> c 127)
  295.                 (logxor c listify-key-sequence-1)
  296.               c)))
  297.         (append key nil))))
  298.  
  299. (defsubst eventp (obj)
  300.   "True if the argument is an event object."
  301.   (or (integerp obj)
  302.       (and (symbolp obj)
  303.        (get obj 'event-symbol-elements))
  304.       (and (consp obj)
  305.        (symbolp (car obj))
  306.        (get (car obj) 'event-symbol-elements))))
  307.  
  308. (defun event-modifiers (event)
  309.   "Returns a list of symbols representing the modifier keys in event EVENT.
  310. The elements of the list may include `meta', `control',
  311. `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
  312. and `down'."
  313.   (let ((type event))
  314.     (if (listp type)
  315.     (setq type (car type)))
  316.     (if (symbolp type)
  317.     (cdr (get type 'event-symbol-elements))
  318.       (let ((list nil))
  319.     (or (zerop (logand type (lsh 1 23)))
  320.         (setq list (cons 'meta list)))
  321.     (or (and (zerop (logand type (lsh 1 22)))
  322.          (>= (logand type 127) 32))
  323.         (setq list (cons 'control list)))
  324.     (or (and (zerop (logand type (lsh 1 21)))
  325.          (= (logand type 255) (downcase (logand type 255))))
  326.         (setq list (cons 'shift list)))
  327.     (or (zerop (logand type (lsh 1 20)))
  328.         (setq list (cons 'hyper list)))
  329.     (or (zerop (logand type (lsh 1 19)))
  330.         (setq list (cons 'super list)))
  331.     (or (zerop (logand type (lsh 1 18)))
  332.         (setq list (cons 'alt list)))
  333.     list))))
  334.  
  335. (defun event-basic-type (event)
  336.   "Returns the basic type of the given event (all modifiers removed).
  337. The value is an ASCII printing character (not upper case) or a symbol."
  338.   (if (consp event)
  339.       (setq event (car event)))
  340.   (if (symbolp event)
  341.       (car (get event 'event-symbol-elements))
  342.     (let ((base (logand event (1- (lsh 1 18)))))
  343.       (downcase (if (< base 32) (logior base 64) base)))))
  344.  
  345. (defsubst mouse-movement-p (object)
  346.   "Return non-nil if OBJECT is a mouse movement event."
  347.   (and (consp object)
  348.        (eq (car object) 'mouse-movement)))
  349.  
  350. (defsubst event-start (event)
  351.   "Return the starting position of EVENT.
  352. If EVENT is a mouse press or a mouse click, this returns the location
  353. of the event.
  354. If EVENT is a drag, this returns the drag's starting position.
  355. The return value is of the form
  356.    (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
  357. The `posn-' functions access elements of such lists."
  358.   (nth 1 event))
  359.  
  360. (defsubst event-end (event)
  361.   "Return the ending location of EVENT.  EVENT should be a click or drag event.
  362. If EVENT is a click event, this function is the same as `event-start'.
  363. The return value is of the form
  364.    (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
  365. The `posn-' functions access elements of such lists."
  366.   (nth (if (consp (nth 2 event)) 2 1) event))
  367.  
  368. (defsubst event-click-count (event)
  369.   "Return the multi-click count of EVENT, a click or drag event.
  370. The return value is a positive integer."
  371.   (if (integerp (nth 2 event)) (nth 2 event) 1))
  372.  
  373. (defsubst posn-window (position)
  374.   "Return the window in POSITION.
  375. POSITION should be a list of the form
  376.    (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
  377. as returned by the `event-start' and `event-end' functions."
  378.   (nth 0 position))
  379.  
  380. (defsubst posn-point (position)
  381.   "Return the buffer location in POSITION.
  382. POSITION should be a list of the form
  383.    (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
  384. as returned by the `event-start' and `event-end' functions."
  385.   (if (consp (nth 1 position))
  386.       (car (nth 1 position))
  387.     (nth 1 position)))
  388.  
  389. (defsubst posn-col-row (position)
  390.   "Return the row and column in POSITION.
  391. POSITION should be a list of the form
  392.    (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
  393. as returned by the `event-start' and `event-end' functions."
  394.   (nth 2 position))
  395.  
  396. (defsubst posn-timestamp (position)
  397.   "Return the timestamp of POSITION.
  398. POSITION should be a list of the form
  399.    (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
  400. as returned by the `event-start' and `event-end' functions."
  401.   (nth 3 position))
  402.  
  403.  
  404. ;;;; Obsolescent names for functions.
  405.  
  406. (defalias 'make-syntax-table 'copy-syntax-table)
  407. (defalias 'dot 'point)
  408. (defalias 'dot-marker 'point-marker)
  409. (defalias 'dot-min 'point-min)
  410. (defalias 'dot-max 'point-max)
  411. (defalias 'window-dot 'window-point)
  412. (defalias 'set-window-dot 'set-window-point)
  413. (defalias 'read-input 'read-string)
  414. (defalias 'send-string 'process-send-string)
  415. (defalias 'send-region 'process-send-region)
  416. (defalias 'show-buffer 'set-window-buffer)
  417. (defalias 'buffer-flush-undo 'buffer-disable-undo)
  418. (defalias 'eval-current-buffer 'eval-buffer)
  419. (defalias 'compiled-function-p 'byte-code-function-p)
  420.  
  421. ;; Some programs still use this as a function.
  422. (defun baud-rate ()
  423.   "Obsolete function returning the value of the `baud-rate' variable.
  424. Please convert your programs to use the variable `baud-rate' directly."
  425.   baud-rate)
  426.  
  427.  
  428. ;;;; Alternate names for functions - these are not being phased out.
  429.  
  430. (defalias 'string= 'string-equal)
  431. (defalias 'string< 'string-lessp)
  432. (defalias 'move-marker 'set-marker)
  433. (defalias 'eql 'eq)
  434. (defalias 'not 'null)
  435. (defalias 'rplaca 'setcar)
  436. (defalias 'rplacd 'setcdr)
  437. (defalias 'beep 'ding) ;preserve lingual purity
  438. (defalias 'indent-to-column 'indent-to)
  439. (defalias 'backward-delete-char 'delete-backward-char)
  440. (defalias 'search-forward-regexp (symbol-function 're-search-forward))
  441. (defalias 'search-backward-regexp (symbol-function 're-search-backward))
  442. (defalias 'int-to-string 'number-to-string)
  443.  
  444. ;;; Should this be an obsolete name?  If you decide it should, you get
  445. ;;; to go through all the sources and change them.
  446. (defalias 'string-to-int 'string-to-number)
  447.  
  448. ;;;; Hook manipulation functions.
  449.  
  450. (defun run-hooks (&rest hooklist)
  451.   "Takes hook names and runs each one in turn.  Major mode functions use this.
  452. Each argument should be a symbol, a hook variable.
  453. These symbols are processed in the order specified.
  454. If a hook symbol has a non-nil value, that value may be a function
  455. or a list of functions to be called to run the hook.
  456. If the value is a function, it is called with no arguments.
  457. If it is a list, the elements are called, in order, with no arguments."
  458.   (while hooklist
  459.     (let ((sym (car hooklist)))
  460.       (and (boundp sym)
  461.        (symbol-value sym)
  462.        (let ((value (symbol-value sym)))
  463.          (if (and (listp value) (not (eq (car value) 'lambda)))
  464.          (mapcar 'funcall value)
  465.            (funcall value)))))
  466.     (setq hooklist (cdr hooklist))))
  467.  
  468. ;; Tell C code how to call this function.
  469. (defconst run-hooks 'run-hooks
  470.   "Variable by which C primitives find the function `run-hooks'.
  471. Don't change it.")
  472.  
  473. (defun add-hook (hook function &optional append)
  474.   "Add to the value of HOOK the function FUNCTION.
  475. FUNCTION is not added if already present.
  476. FUNCTION is added (if necessary) at the beginning of the hook list
  477. unless the optional argument APPEND is non-nil, in which case
  478. FUNCTION is added at the end.
  479.  
  480. HOOK should be a symbol, and FUNCTION may be any valid function.  If
  481. HOOK is void, it is first set to nil.  If HOOK's value is a single
  482. function, it is changed to a list of functions."
  483.   (or (boundp hook) (set hook nil))
  484.   ;; If the hook value is a single function, turn it into a list.
  485.   (let ((old (symbol-value hook)))
  486.     (if (or (not (listp old)) (eq (car old) 'lambda))
  487.     (set hook (list old))))
  488.   (or (if (consp function)
  489.       ;; Clever way to tell whether a given lambda-expression
  490.       ;; is equal to anything in the hook.
  491.       (let ((tail (assoc (cdr function) (symbol-value hook))))
  492.         (equal function tail))
  493.     (memq function (symbol-value hook)))
  494.       (set hook 
  495.        (if append
  496.            (nconc (symbol-value hook) (list function))
  497.          (cons function (symbol-value hook))))))
  498.  
  499. (defun remove-hook (hook function)
  500.   "Remove from the value of HOOK the function FUNCTION.
  501. HOOK should be a symbol, and FUNCTION may be any valid function.  If
  502. FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
  503. list of hooks to run in HOOK, then nothing is done.  See `add-hook'."
  504.   (if (or (not (boundp hook))        ;unbound symbol, or
  505.       (null (symbol-value hook))    ;value is nil, or
  506.       (null function))        ;function is nil, then
  507.       nil                ;Do nothing.
  508.     (let ((hook-value (symbol-value hook)))
  509.       (if (consp hook-value)
  510.       (setq hook-value (delete function hook-value))
  511.     (if (eq hook-value function)
  512.         (setq hook-value nil)))
  513.       (set hook hook-value))))
  514.  
  515. ;;;; Specifying things to do after certain files are loaded.
  516.  
  517. (defun eval-after-load (file form)
  518.   "Arrange that, if FILE is ever loaded, FORM will be run at that time.
  519. This makes or adds to an entry on `after-load-alist'.
  520. FILE should be the name of a library, with no directory name."
  521.   (or (assoc file after-load-alist)
  522.       (setq after-load-alist (cons (list file) after-load-alist)))
  523.   (nconc (assoc file after-load-alist) (list form))
  524.   form)
  525.  
  526. (defun eval-next-after-load (file)
  527.   "Read the following input sexp, and run it whenever FILE is loaded.
  528. This makes or adds to an entry on `after-load-alist'.
  529. FILE should be the name of a library, with no directory name."
  530.   (eval-after-load file (read)))
  531.  
  532.  
  533. ;;;; Input and display facilities.
  534.  
  535. (defun read-quoted-char (&optional prompt)
  536.   "Like `read-char', except that if the first character read is an octal
  537. digit, we read up to two more octal digits and return the character
  538. represented by the octal number consisting of those digits.
  539. Optional argument PROMPT specifies a string to use to prompt the user."
  540.   (let ((count 0) (code 0) char)
  541.     (while (< count 3)
  542.       (let ((inhibit-quit (zerop count))
  543.         (help-form nil))
  544.     (and prompt (message "%s-" prompt))
  545.     (setq char (read-char))
  546.     (if inhibit-quit (setq quit-flag nil)))
  547.       (cond ((null char))
  548.         ((and (<= ?0 char) (<= char ?7))
  549.          (setq code (+ (* code 8) (- char ?0))
  550.            count (1+ count))
  551.          (and prompt (message (setq prompt
  552.                     (format "%s %c" prompt char)))))
  553.         ((> count 0)
  554.          (setq unread-command-events (list char) count 259))
  555.         (t (setq code char count 259))))
  556.     (logand 255 code)))
  557.  
  558. (defun force-mode-line-update (&optional all)
  559.   "Force the mode-line of the current buffer to be redisplayed.
  560. With optional non-nil ALL then force then force redisplay of all mode-lines."
  561.   (if all (save-excursion (set-buffer (other-buffer))))
  562.   (set-buffer-modified-p (buffer-modified-p)))
  563.  
  564. (defun momentary-string-display (string pos &optional exit-char message) 
  565.   "Momentarily display STRING in the buffer at POS.
  566. Display remains until next character is typed.
  567. If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
  568. otherwise it is then available as input (as a command if nothing else).
  569. Display MESSAGE (optional fourth arg) in the echo area.
  570. If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
  571.   (or exit-char (setq exit-char ?\ ))
  572.   (let ((buffer-read-only nil)
  573.     (modified (buffer-modified-p))
  574.     (name buffer-file-name)
  575.     insert-end)
  576.     (unwind-protect
  577.     (progn
  578.       (save-excursion
  579.         (goto-char pos)
  580.         ;; defeat file locking... don't try this at home, kids!
  581.         (setq buffer-file-name nil)
  582.         (insert-before-markers string)
  583.         (setq insert-end (point))
  584.         ;; If the message end is off screen, recenter now.
  585.         (if (> (window-end) insert-end)
  586.         (recenter (/ (window-height) 2)))
  587.         ;; If that pushed message start off the screen,
  588.         ;; scroll to start it at the top of the screen.
  589.         (move-to-window-line 0)
  590.         (if (> (point) pos)
  591.         (progn
  592.           (goto-char pos)
  593.           (recenter 0))))
  594.       (message (or message "Type %s to continue editing.")
  595.            (single-key-description exit-char))
  596.       (let ((char (read-event)))
  597.         (or (eq char exit-char)
  598.         (setq unread-command-events (list char)))))
  599.       (if insert-end
  600.       (save-excursion
  601.         (delete-region pos insert-end)))
  602.       (setq buffer-file-name name)
  603.       (set-buffer-modified-p modified))))
  604.  
  605.  
  606. ;;;; Miscellanea.
  607.  
  608. (defun ignore (&rest ignore) 
  609.   "Do nothing.
  610. Accept any number of arguments, but ignore them."
  611.   nil)
  612.  
  613. (defun error (&rest args)
  614.   "Signal an error, making error message by passing all args to `format'."
  615.   (while t
  616.     (signal 'error (list (apply 'format args)))))
  617.  
  618. (defun user-original-login-name ()
  619.   "Return user's login name from original login.
  620. This tries to remain unaffected by `su', by looking in environment variables."
  621.   (or (getenv "LOGNAME") (getenv "USER") (user-login-name)))
  622.  
  623. (defun start-process-shell-command (name buffer &rest args)
  624.   "Start a program in a subprocess.  Return the process object for it.
  625. Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
  626. NAME is name for process.  It is modified if necessary to make it unique.
  627. BUFFER is the buffer or (buffer-name) to associate with the process.
  628.  Process output goes at end of that buffer, unless you specify
  629.  an output stream or filter function to handle the output.
  630.  BUFFER may be also nil, meaning that this process is not associated
  631.  with any buffer
  632. Third arg is command name, the name of a shell command.
  633. Remaining arguments are the arguments for the command.
  634. Wildcards and redirection are handle as usual in the shell."
  635.   (if (eq system-type 'vax-vms)
  636.       (apply 'start-process name buffer args)
  637.     (start-process name buffer shell-file-name "-c"
  638.            (concat "exec " (mapconcat 'identity args " ")))))
  639.  
  640. (defmacro save-match-data (&rest body)
  641.   "Execute the BODY forms, restoring the global value of the match data."
  642.   (let ((original (make-symbol "match-data")))
  643.     (list
  644.      'let (list (list original '(match-data)))
  645.      (list 'unwind-protect
  646.            (cons 'progn body)
  647.            (list 'store-match-data original)))))
  648.  
  649. ;; now in fns.c
  650. ;(defun nth (n list)
  651. ;  "Returns the Nth element of LIST.
  652. ;N counts from zero.  If LIST is not that long, nil is returned."
  653. ;  (car (nthcdr n list)))
  654. ;
  655. ;(defun copy-alist (alist)
  656. ;  "Return a copy of ALIST.
  657. ;This is a new alist which represents the same mapping
  658. ;from objects to objects, but does not share the alist structure with ALIST.
  659. ;The objects mapped (cars and cdrs of elements of the alist)
  660. ;are shared, however."
  661. ;  (setq alist (copy-sequence alist))
  662. ;  (let ((tail alist))
  663. ;    (while tail
  664. ;      (if (consp (car tail))
  665. ;      (setcar tail (cons (car (car tail)) (cdr (car tail)))))
  666. ;      (setq tail (cdr tail))))
  667. ;  alist)
  668.  
  669. ;;; subr.el ends here
  670.  
  671.